package com.cfpamf.ms.insur.pay.controller;

import com.alibaba.fastjson.JSON;
import com.cfpamf.ms.insur.pay.service.InsurNotifyService;
import com.cfpamf.ms.insur.pay.util.HttpRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 保险第三方回调接口
 *
 * @author zhangnayi
 */
@Slf4j
@Api(value = "保险第三方回调接口", tags = {"保险第三方回调接口"})
@RequestMapping("/insurNotify")
@RestController
public class InsurNotifyController {

    /**
     * 保险支付回调service
     */
    @Autowired
    private InsurNotifyService insurNotifyService;

    /**
     * 支付宝支付成功异步回调接口
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "支付宝支付成功异步回调接口")
    @PostMapping("/aliPay/{sourceSys}/pay")
    public String aliPayCallbackNotify(@PathVariable String sourceSys, HttpServletRequest request) {
        log.info("【支付宝支付回调】<<<< sourceSys={},参数 params={}", sourceSys, JSON.toJSONString(HttpRequestUtil.getParameterMap(request)));
        String respStr = insurNotifyService.aliPayCallbackNotify(sourceSys, request);
        log.info("【支付宝支付回调】>>>> sourceSys={},响应 result={}", sourceSys, respStr);
        return respStr;
    }

    /**
     * 微信支付成功异步回调接口
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "微信支付成功异步回调接口")
    @PostMapping("/wxPay/{sourceSys}/pay")
    public String wxPayCallbackNotify(@PathVariable String sourceSys, @RequestBody(required = false) String notifyXml, HttpServletRequest request) {
        log.info("【微信支付回调】<<<< sourceSys={}, 参数 params={}", sourceSys, notifyXml);
        request.setAttribute("notifyXml", notifyXml);
        String respStr = insurNotifyService.wxPayCallbackNotify(sourceSys, request);
        log.info("【微信支付回调】>>>> sourceSys={}, 响应 result={}", sourceSys, respStr);
        return respStr;
    }

    /**
     * 支付宝退款成功异步回调接口
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "支付宝退款成功异步回调接口")
    @PostMapping("/aliPay/{sourceSys}/refund")
    public String aliRefundCallbackNotify(@PathVariable String sourceSys, HttpServletRequest request) {
        log.info("【支付宝退款回调】<<<< sourceSys={}, 参数 params={}", sourceSys, JSON.toJSONString(HttpRequestUtil.getParameterMap(request)));
        log.info("【支付宝退款回调】>>>> sourceSys={}, 响应 result={}", sourceSys, "");
        return "";
    }

    /**
     * 微信退款成功异步回调接口
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "微信退款成功异步回调接口")
    @PostMapping("/wxPay/{sourceSys}/refund")
    public String wxRefundCallbackNotify(@PathVariable String sourceSys, @RequestBody(required = false) String notifyXml, HttpServletRequest request) {
        log.info("【微信退款回调】<<<< sourceSys={}, 参数 params={}", sourceSys, notifyXml);
        request.setAttribute("notifyXml", notifyXml);
        String respStr = insurNotifyService.wxRefundCallbackNotify(sourceSys, request);
        log.info("【微信退款回调】>>>> sourceSys={}, 响应 result={}", sourceSys, respStr);
        return respStr;
    }
}
